<template>
  <base-modal
    :show.sync="visible"
    :title="title"
    width="528px"
    @close="visible = false"
  >
    <div class="pad-20">
      <div class="mar-b-20">{{$t('serviceProvider.confirmResetPassword')}}</div>
      <el-form
        label-position="top"
        
        ref="form"
        :model="form"
        :rules="rules"
      >
        <el-form-item :label="`${$t('serviceProvider.newPassword')}：`" required prop="passward">
          <el-input v-model="form.passward" show-password type="password"></el-input>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button type="plain-third" @click="visible = false">{{$t('common.base.cancel')}}</el-button>
      <el-button type="primary" @click="validate" :disabled="pending"
        >{{$t('common.base.confirm')}}</el-button
      >
    </div>
  </base-modal>
</template>

<script>
import { resetPwdUpdateAdmin } from '@src/api/DepartmentApi';
import { ValidatePassWordReg } from '@src/model/const/Reg';
import { PASS_WORD_MESSAGE } from '@src/model/const/Alert';
import md5 from 'js-md5';
export default {
  name: 'ResetPassword',
  data() {
    return {
      pending: false,
      title: this.$t('common.base.resetPwd'),
      visible: false,
      form: {
        passward: '',
      },
      rules: {
        passward: [
          {
            required: true,
            validator: this.validatorPassward,
            trigger: ['blur', 'change'],
          },
        ],
      },
      userId: '',
    };
  },
  methods: {
    openDialog(userId) {
      this.$refs['form'].resetFields();
      this.form = {
        passward: '',
      };
      this.visible = true;
      this.userId = userId;
    },
    // 校验密码
    async validatorPassward(rule, value, cb) {
      if (!value.trim()) {
        cb(new Error(this.$t('common.base.inputPassword')));
      }
      if (value && !ValidatePassWordReg.test(value)) {
        cb(new Error(PASS_WORD_MESSAGE));
      }
      return cb();
    },
    validate() {
      this.$refs.form.validate(vaild => {
        if (vaild) {
          this.rest();
        }
      });
    },
    async rest() {
      let data = {
        userId: this.userId,
        password: md5(this.form?.passward),
      };
      try {
        let res = await resetPwdUpdateAdmin(data);
        this.$message({
          type: res.success ? 'success' : 'error',
          message: res.message,
          showClose: true,
          duration: 2000,
        });
      } catch (e) {
        console.error(e);
      } finally {
        this.visible = false;
      }
    },
  },
};
</script>

<style scoped></style>
